
local getMark = function (player)
  local mark = player:getMark("emo__luoxue_effect")
  if type(mark) ~= "table" then
    return {"emo__luoxue_losehp", "emo__luoxue_draw", "emo__luoxue_recover"}
  end
  return mark
end

local skel = fk.CreateSkill {
  name = "emo__luoxue",
  dynamic_desc = function (self, player, lang)
    return "emo__luoxue_inner:" .. table.concat(getMark(player), ":")
  end,
}

Fk:loadTranslationTable{
  ["emo__luoxue"] = "落雪",
  [":emo__luoxue"] = "出牌阶段，你可以执行以下前任意项，然后将这些项改为“弃一张牌”。若你因此执行了“弃一张牌”，此技能本回合失效。"..
  "<br>1.失去一点体力；2.摸三张牌；3.回复一点体力。",

  [":emo__luoxue_inner"] = "出牌阶段，你可以执行以下前任意项，并将这些项改为“弃一张牌”。若你因此执行了“弃一张牌”，此技能本回合失效。"..
  "<br>1.{1}；2.{2}；3.{3}。",

  ["#emo__luoxue"] = "落雪：你可以执行前任意项：%arg；%arg2；%arg3",
  ["emo__luoxue_discard"] = "弃一张牌",
  ["emo__luoxue_losehp"] = "失去一点体力",
  ["emo__luoxue_draw"] = "摸三张牌",
  ["emo__luoxue_recover"] = "回复一点体力",
  ["EmoLuoXue1"] = "%arg",
  ["EmoLuoXue2"] = "%arg；%arg2",
  ["EmoLuoXue3"] = "%arg；%arg2；%arg3",

  ["$emo__luoxue1"] = "此生若雪，染天地之洁，启三春之引。", -- 若雪
  ["$emo__luoxue2"] = "心怀莺飞之暖，何惧落雪之寒。", -- 梦雪
}

skel:addEffect("active", {
  anim_type = "drawcard",
  prompt = function (self, player)
    return "#emo__luoxue:::" .. table.concat(getMark(player), ":")
  end,
  card_num = 0,
  target_num = 0,
  interaction = function(self, player)
    local choices = {}
    local mark = getMark(player)
    for i = 1, 3 do
      local args = table.slice(mark, 1, i + 1)
      choices[i] = "EmoLuoXue" .. i .. ":::" .. table.concat(args, ":")
    end
    if mark[1] == "emo__luoxue_discard" and player:isNude() then return end -- 没牌还想空发？
    return UI.ComboBox { choices = choices }
  end,
  feasible = function (self)
    return self.interaction.data ~= nil
  end,
  can_use = function(self, player)
    return true
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local choice = effect.interaction_data
    if type(choice) ~= "string" then return end
    local splitter = choice:split(":")
    local num = tonumber(splitter[1]:sub(-1, -1)) -- 执行前几项
    local choices = table.slice(splitter, 4, 4 + num)
    local invalid = false
    for _, ch in ipairs(choices) do
      if player.dead then return end
      if ch == "emo__luoxue_draw" then
        player:drawCards(3, skel.name)
      elseif ch == "emo__luoxue_losehp" then
        room:loseHp(player, 1, skel.name)
      elseif ch == "emo__luoxue_recover" then
        room:recover { num = 1, skillName = skel.name, who = player, recoverBy = player }
      else
        invalid = true
        room:askToDiscard(player, {min_num = 1, max_num = 1, include_equip = true, skill_name = skel.name, cancelable = false})
      end
    end
    if not player.dead then
      if invalid then
        room:invalidateSkill(player, skel.name, "-turn")
      end
      local mark = getMark(player)
      for i = 1, num do
        mark[i] = "emo__luoxue_discard"
      end
      room:setPlayerMark(player, "emo__luoxue_effect", mark)
    end
  end,
})

return skel
